﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RestSharp;

namespace ElementTestApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }


        [HttpGet]
        [Route("weather")]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }


        public class StateInfo
        {
            public string Name { get; set; }
            public string Address { get; set; }
            public string Tel { get; set; }
            public int Sex { get; set; }
        }


        public class GETInfo
        {
            public List<StateInfo> stainfo { get; set; }
            public int total { get; set; }//数据总数
        }

        public class PageFY
        {
            public int pagesize { get; set; }
            public int pagenum { get; set; }
        }

        [HttpPost]
        [Route("testinfo")]
        //public GETInfo TestInfo(int? pagenum = 0, int? pagesize = 3)
        public GETInfo TestInfo([FromBody] PageFY param)
        {
            #region 假数据
            StateInfo info = new StateInfo()
            {
                Name = "1111",
                Address = "qwerrr",
                Tel = "123456789",
                Sex = 0
            };

            StateInfo info1 = new StateInfo()
            {
                Name = "22222",
                Address = "poilkjmnb",
                Tel = "987654321",
                Sex = 1
            };

            StateInfo info2 = new StateInfo()
            {
                Name = "333333",
                Address = "poilkjmnb",
                Tel = "987654321",
                Sex = 1
            };

            StateInfo info3 = new StateInfo()
            {
                Name = "44445555",
                Address = "公司feeder苟富贵",
                Tel = "987654321",
                Sex = 1
            };

            StateInfo info4 = new StateInfo()
            {
                Name = "444444",
                Address = "现在才的身份得分",
                Tel = "987654321",
                Sex = 0
            };

            StateInfo info5 = new StateInfo()
            {
                Name = "55555",
                Address = "能否哈达时间",
                Tel = "4445559989",
                Sex = 1
            };

            #endregion

            object obj = Request.Query["param"];

            //pagesize = Convert.ToInt32(Request.Query["pagesize"]);
            //pagenum = Convert.ToInt32(Request.Query["pagenum"]);

            List<StateInfo> stainfo = new List<StateInfo>();

            stainfo.Add(info);
            stainfo.Add(info1);
            stainfo.Add(info2);
            stainfo.Add(info3);
            stainfo.Add(info4);
            stainfo.Add(info5);

            int count = stainfo.Count;

            stainfo = stainfo.Take((int)param.pagesize).Skip((int)param.pagenum * (int)param.pagesize).ToList();


            //string json = stainfo.ToString();
            string json = JsonConvert.SerializeObject(stainfo);

            GETInfo gETInfo = new GETInfo()
            {
                //stainfo = stainfo.ToArray(),
                stainfo = stainfo,
                total = count
            };

            return gETInfo;
        }


        [HttpGet]
        [Route("testinfojson")]
        //public GETInfo TestInfoJson([FromBody] int? pagenum = 0, int? pagesize = 3)
        public GETInfo TestInfoJson([FromQuery] PageFY? page)
        {
            #region 假数据
            StateInfo info = new StateInfo()
            {
                Name = "1111",
                Address = "qwerrr",
                Tel = "123456789",
                Sex = 0
            };

            StateInfo info1 = new StateInfo()
            {
                Name = "22222",
                Address = "poilkjmnb",
                Tel = "987654321",
                Sex = 1
            };

            StateInfo info2 = new StateInfo()
            {
                Name = "333333",
                Address = "poilkjmnb",
                Tel = "987654321",
                Sex = 1
            };

            StateInfo info3 = new StateInfo()
            {
                Name = "44445555",
                Address = "公司feeder苟富贵",
                Tel = "987654321",
                Sex = 1
            };

            StateInfo info4 = new StateInfo()
            {
                Name = "444444",
                Address = "现在才的身份得分",
                Tel = "987654321",
                Sex = 0
            };

            StateInfo info5 = new StateInfo()
            {
                Name = "55555",
                Address = "能否哈达时间",
                Tel = "4445559989",
                Sex = 1
            };

            #endregion

            List<StateInfo> stainfo = new List<StateInfo>();

            //pagesize = Convert.ToInt32(Request.Query["pagesize"]);
            //pagenum = Convert.ToInt32(Request.Query["pagenum"]);

            stainfo.Add(info);
            stainfo.Add(info1);
            stainfo.Add(info2);
            stainfo.Add(info3);
            stainfo.Add(info4);
            stainfo.Add(info5);

            int count = stainfo.Count;

            stainfo = stainfo.Take((int)page.pagesize).Skip((int)page.pagenum * (int)page.pagesize).ToList();


            GETInfo gETInfo = new GETInfo()
            {
                //stainfo = stainfo.ToArray(),
                stainfo = stainfo,
                total = count
            };

            return gETInfo;
        }

    }
}
